package org.seckill.TestSpring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Created by wangyg 
 * on 2017/4/26 14:53
 */
public class ApplicationContextAwareHelper implements ApplicationContextAware/*,InitializingBean*/ {
	static ApplicationContext context;

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.context = applicationContext;
	}

	public Object getBean(String name) {
		return context.getBean(name);
	}

	public static <T> T getBean(String name, Class<T> clz) {
		if (context == null) {
			throw new NullPointerException("ApplicationContext is null!");
		}
		return (T) context.getBean(name);
	}

//	public void afterPropertiesSet() throws Exception {
//		this.context = applicationContext;
//	}
}
